﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Common.Views;

namespace DataAccess
{
    public class FaultsRepository : ConnectionClass
    {
        public FaultsRepository() : base() { }

        public IEnumerable<Fault> GetFaultStatus()
        {
            return entities.Faults.AsQueryable();
        }

        public IEnumerable<FaultDetailsView> GetFilteredFaultDetailsForUser(string username, string fromDate, string toDate, int productID)
        {
            DateTime FDate = Convert.ToDateTime(fromDate);
            DateTime TDate = Convert.ToDateTime(toDate);
            return (
                                      from uf in entities.UserFaults
                                      join f in entities.Faults on uf.Status_FK equals f.ID
                                      where uf.Username_FK == username && uf.Date >= FDate && uf.Date <= TDate && uf.Product_FK == productID

                                      select new FaultDetailsView()
                                      {
                                          FaultID = uf.ID,
                                          TicketNo = uf.FaultID,
                                          Date = uf.Date,
                                          FaultDetails = uf.Details,
                                          Status = f.Status,
                                          productID = uf.Product_FK
                                      }
                                );  


        }
        
        public IEnumerable<FaultDetailsView> GetFaultDetailsForAdmin(string username)
        {
            return (
                           from uf in entities.UserFaults
                           join f in entities.Faults on uf.Status_FK equals f.ID
                           where uf.Username_FK != username 
                           
                           select new FaultDetailsView()
                           {
                               FaultID = uf.ID,
                               TicketNo = uf.FaultID,
                               Date = uf.Date,
                               FaultDetails = uf.Details,
                               Status = f.Status,
                               StatusID = f.ID,
                               Username = uf.Username_FK
                           }
                     );
        }

        public IEnumerable<FaultDetailsView> GetFaultDetailsForUser(string username)
        {
            return (
                           from uf in entities.UserFaults
                           join f in entities.Faults on uf.Status_FK equals f.ID
                           where uf.Username_FK == username 

                           select new FaultDetailsView()
                           {
                               FaultID = uf.ID,
                               TicketNo = uf.FaultID,
                               Date = uf.Date,
                               FaultDetails = uf.Details,
                               Status = f.Status,
                               StatusID = f.ID,
                               Username = uf.Username_FK
                           }
                     );            
        }

        public IEnumerable<FaultDetailsView> GetFaultDetailsForPDF(string username, int ticketNo)
        {
            return (
                           from uf in entities.UserFaults
                           join f in entities.Faults on uf.Status_FK equals f.ID
                           where uf.Username_FK == username && uf.FaultID == ticketNo

                           select new FaultDetailsView()
                           {
                               FaultID = uf.ID,
                               TicketNo = uf.FaultID,
                               Date = uf.Date,
                               FaultDetails = uf.Details,
                               Status = f.Status
                           }
                     );
        }

        public void SubmitFault(UserFault uf)
        {
            entities.UserFaults.AddObject(uf);
            entities.SaveChanges();
        }

        public IEnumerable<FaultDetailsView> AdvancedFaultSearch(string username, int tokenNumber, string fromD, string toDate)
        {
            IEnumerable<FaultDetailsView> allFaults = GetFaultDetailsForUser(username).ToList();
                                     
                        
            if (fromD != "" && toDate != "")
            {
                allFaults = allFaults.Intersect(allFaults.Where(x => x.Username == username && x.StatusID == tokenNumber && x.Date >= Convert.ToDateTime(fromD) && x.Date <= Convert.ToDateTime(toDate)));      
            }
            else
            {
               allFaults= allFaults.Intersect(allFaults.Where(x => x.Username == username && x.StatusID == tokenNumber));
            }
            return allFaults;
        }

        public Fault GetFaultIDFromName(string fName)
        {
            return entities.Faults.SingleOrDefault(f => f.Status == fName);
        }

        public UserFault GetFaultByID(int id)
        {
            return entities.UserFaults.SingleOrDefault(uf => uf.ID == id);
        }

        public UserFault GetFaultsFirstDateByTokenNumber(int tn)
        {
            return entities.UserFaults.Where(uf => uf.FaultID == tn).FirstOrDefault();
        }

        public IQueryable<UserFault> GetTokenNumbers()
        {
            return entities.UserFaults.AsQueryable();
        }

        public IEnumerable<FaultDetailsView> GetAllFaultsForThisProduct(int pID, string username)
        {
            return (
                           from uf in entities.UserFaults
                           join f in entities.Faults on uf.Status_FK equals f.ID
                           join p in entities.Products on uf.Product_FK equals p.ID
                           where uf.Username_FK == username && uf.Product_FK == pID

                           select new FaultDetailsView()
                           {
                               FaultID = uf.ID,
                               TicketNo = uf.FaultID,
                               Date = uf.Date,
                               FaultDetails = uf.Details,
                               Status = f.Status,
                               productID = uf.Product_FK                               
                           }
                     );
        }

        public IEnumerable<FaultDetailsView> GetFaultByTokenNumber(int tn, string username)
        {
            return (
                           from uf in entities.UserFaults
                           join f in entities.Faults on uf.Status_FK equals f.ID
                           where uf.Username_FK == username && uf.FaultID == tn

                           select new FaultDetailsView()
                           {
                               FaultID = uf.ID,
                               TicketNo = uf.FaultID,
                               Date = uf.Date,
                               FaultDetails = uf.Details,
                               Status = f.Status
                           }
                     );
        }

        public void UpdateFault(UserFault newUF)
        {
            entities.UserFaults.AddObject(newUF);
            entities.SaveChanges();
        }

        public IEnumerable<FaultDetailsView> FilterFaultsByDate()
        {
            return (
                           from uf in entities.UserFaults
                           join f in entities.Faults on uf.Status_FK equals f.ID
                           where uf.Date != null
                           select new FaultDetailsView()
                           {
                               FaultID = uf.ID,
                               TicketNo = uf.FaultID,
                               Date = uf.Date,
                               FaultDetails = uf.Details,
                               Status = f.Status
                           }
                     ).OrderByDescending(uf=> uf.Date);
        }

        public IEnumerable<FaultDetailsView> FilterFaultsByDateForAdmin(string admin)
        {
            return (
                           from uf in entities.UserFaults
                           join f in entities.Faults on uf.Status_FK equals f.ID
                           where uf.Date != null && uf.Username_FK != admin
                           select new FaultDetailsView()
                           {
                               FaultID = uf.ID,
                               TicketNo = uf.FaultID,
                               Date = uf.Date,
                               FaultDetails = uf.Details,
                               Status = f.Status
                           }
                     ).OrderByDescending(uf => uf.Date);
        }
       

    }
}
